package controller;
import java.awt.Color;
import java.util.List;
import java.util.Random;

import view.SimulatorView;
import model.*;

public class PopulationGenerator {

    // The probability that a fox will be created in any given grid position.
    private static final double FOX_CREATION_PROBABILITY = 0.02;
    // The probability that a rabbit will be created in any given grid position.
    private static final double RABBIT_CREATION_PROBABILITY = 0.08;    
    // The probability that a wolf will be created in any given grid position.
    private static final double WOLF_CREATION_PROBABILITY = 0.01;    
    // The probability that a bear will be created in any given grid position.
    private static final double BEAR_CREATION_PROBABILITY = 0.01;
    // The probability that grass will be created in any given grid position.
    private static final double GRASS_CREATION_PROBABILITY = 0.50;   
	

    // List of animals in the field.
    private List<Actor> animals;
    // The current state of the field.
    private Field field;
    // The current step of the simulation.
    private SimulatorView view;
    // The population generator
    
	public PopulationGenerator(SimulatorView view, Field field, List<Actor> animals) {
		this.view = view;
		this.field = field;
		this.animals = animals;
		
        view.setColor(Rabbit.class, Color.ORANGE);
        view.setColor(Fox.class, Color.BLUE);
        view.setColor(Wolf.class, Color.MAGENTA);
        view.setColor(Bear.class, Color.BLACK);
        view.setColor(ModifierGrass.class, Color.GREEN);
	}

    /**
     * Randomly populate the field with foxes and rabbits.
     */
    public void populate()
    {
        Random rand = Randomizer.getRandom();
        field.clear();
        for(int row = 0; row < field.getDepth(); row++) {
            for(int col = 0; col < field.getWidth(); col++) {
            	if(rand.nextDouble() <= BEAR_CREATION_PROBABILITY) {
            		Location location = new Location(row, col);
            		Bear bear = new Bear(true, field, location);
            		animals.add(bear);
            	}
            	else if(rand.nextDouble() <= WOLF_CREATION_PROBABILITY) {
            		Location location = new Location(row, col);
            		Wolf wolf = new Wolf(true, field, location);
            		animals.add(wolf);
            	}
            	else if(rand.nextDouble() <= FOX_CREATION_PROBABILITY) {
                    Location location = new Location(row, col);
                    Fox fox = new Fox(true, field, location);
                    animals.add(fox);
                }
                else if(rand.nextDouble() <= RABBIT_CREATION_PROBABILITY) {
                    Location location = new Location(row, col);
                    Rabbit rabbit = new Rabbit(true, field, location);
                    animals.add(rabbit);
                }
                else if(rand.nextDouble() <= GRASS_CREATION_PROBABILITY) {
                	Location location = new Location(row, col);
                	ModifierGrass grass = new ModifierGrass(true, field, location);
                	animals.add(grass);
                }
            	
            	
                // else leave the location empty.
            }
        }
    }
}
